/****************************************************************************
**
** Copyright (C) 2009 by user *
** user@greenphonesdk *
**
** Contact: Qt Extended Information (info@qtextended.org)
**
** This file may be used under the terms of the GNU General Public License
** version 2.0 as published by the Free Software Foundation and appearing
** in the file LICENSE.GPL included in the packaging of this file.
**
** Please review the following information to ensure GNU General Public
** Licensing requirements will be met:
**     http://www.fsf.org/licensing/licenses/info/GPLv2.html.
**
**
****************************************************************************/

#include "assignment_1.h"
#include <QDebug>

/*
 *  Constructs the dialog which is a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'
 */
Assignment1::Assignment1(QWidget *parent, Qt::WFlags f)
    : QWidget(parent, f)
{
    // reads the .ui file to determine widgets and layout
    setupUi(this);

    // sets the label for Qt to  auto-fill
    label->setAutoFillBackground(true);

    // initialise counter to zero
    counter = 0;

    // instantiate hidden counter label
    countLabel = new QLabel(this);
    countLabel->setVisible(false);
    countLabel->setText(QString::number(counter));

    // instantiate new button
    QPushButton *YellowButton = new QPushButton("Yellow");
    myLayout->insertWidget(4, YellowButton);
    // connect the new button
    QObject::connect(YellowButton, SIGNAL(clicked()), this, SLOT(setYellowMode()));
    QObject::connect(this, SIGNAL(signalYellowMode()), this, SLOT(updateCounter()));
}

/*
 *  Destroys the object and frees any allocated resources
 */
Assignment1::~Assignment1()
{
    // no need to delete child widgets, Qt does it all for us
}

/*
 *  Updates the global count variable and updates the hidden label
 */
void Assignment1::updateCounter()
{
    counter++;
    countLabel->setText(QString::number(counter));
}

/*
 *  Increments counter and sets the label to red
 */
void Assignment1::on_RedButton_clicked()
{
    updateCounter();

    label->setText("Red Mode");
    label->setPalette(QPalette(Qt::red));

    qDebug() << "Red Mode: " << QString::number(counter);
}
  
/*
 *  Increments counter and sets the label to blue
 */
void Assignment1::on_BlueButton_clicked()
{
    updateCounter();

    label->setText("Blue Mode");
    label->setPalette(QPalette(Qt::blue));
    
    qDebug() << "Blue Mode: " << QString::number(counter);
}

/*
 *  Increments counter and sets the label to green
 */
void Assignment1::on_GreenButton_clicked()
{
    updateCounter();

    label->setText("Green Mode");
    label->setPalette(QPalette(Qt::green));
    
    qDebug() << "Green Mode: " << QString::number(counter);
}

/*
 *  Increments counter and sets the label to yellow
 */
void Assignment1::setYellowMode()
{
    emit signalYellowMode();
    emit signalYellowMode();
    emit signalYellowMode();
    emit signalYellowMode();
    emit signalYellowMode();

    label->setText("Yellow Mode");
    label->setPalette(QPalette(Qt::yellow));
    
    qDebug() << "Yellow Mode: " << QString::number(counter);
}

/*
 *  Displays a messagebox with the hidden label value and exits
 */
void Assignment1::on_QuitButton_clicked()
{
    qDebug() << "Quitting: " << QString::number(counter);

    QMessageBox msgBox;
    msgBox.setWindowTitle("Counter Value");
    msgBox.setIcon(QMessageBox::Information);
    msgBox.setText("Value: " + countLabel->text() + "\nQuit?");

    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    switch (msgBox.exec()) {
    case QMessageBox::Yes:
        close();
    case QMessageBox::No:
        break;
    default:
        break;
    }
}

